package com.castle.fortress.admin.member.rest;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.entity.MemberCollectEntity;
import com.castle.fortress.admin.member.dto.MemberCollectDto;
import com.castle.fortress.admin.member.service.MemberCollectService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 会员收藏表 api 控制器
 *
 * @author castle
 * @since 2023-02-14
 */
@Api(tags="会员收藏表api管理控制器")
@RestController
@RequestMapping("/api/member/studyCollect")
public class ApiMemberCollectController {
    @Autowired
    private MemberCollectService memberCollectService;


    /**
     * 会员收藏表的分页展示
     * @param memberCollectDto 会员收藏表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="会员收藏表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员收藏表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<MemberCollectDto>> pageStudyCollect(MemberCollectDto memberCollectDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberCollectDto> page = new Page(pageIndex, pageSize);

        IPage<MemberCollectDto> pages = memberCollectService.pageStudyCollect(page, memberCollectDto);
        return RespBody.data(pages);
    }

    /**
     * 会员收藏表保存
     * @param memberCollectDto 会员收藏表实体类
     * @return
     */
    @CastleLog(operLocation="会员收藏表-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("会员收藏表保存")
    @PostMapping("/save")
    public RespBody<String> saveStudyCollect(@RequestBody MemberCollectDto memberCollectDto){
        if(memberCollectDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        Long memberId = WebUtil.currentMember().getId();
        memberCollectDto.setCollectTime(new Date());
            MemberCollectEntity studyCollectEntity = ConvertUtil.transformObj(memberCollectDto, MemberCollectEntity.class);
        if(memberCollectService.save(studyCollectEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员收藏表编辑
     * @param memberCollectDto 会员收藏表实体类
     * @return
     */
    @CastleLog(operLocation="会员收藏表-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("会员收藏表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateStudyCollect(@RequestBody MemberCollectDto memberCollectDto){
        if(memberCollectDto == null || memberCollectDto.getId() == null || memberCollectDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberCollectEntity studyCollectEntity = ConvertUtil.transformObj(memberCollectDto, MemberCollectEntity.class);
        if(memberCollectService.updateById(studyCollectEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员收藏表删除
     * @param ids 会员收藏表id集合
     * @return
     */
    @CastleLog(operLocation="会员收藏表-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("会员收藏表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteStudyCollect(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberCollectService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员收藏表详情
     * @param id 会员收藏表id
     * @return
     */
    @CastleLog(operLocation="会员收藏表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员收藏表详情")
    @GetMapping("/info")
    public RespBody<MemberCollectDto> infoStudyCollect(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberCollectEntity studyCollectEntity = memberCollectService.getById(id);
            MemberCollectDto memberCollectDto = ConvertUtil.transformObj(studyCollectEntity, MemberCollectDto.class);
        return RespBody.data(memberCollectDto);
    }

    /**
     * 收藏或取消收藏
     * @return
     */
    @ApiOperation("收藏或取消收藏")
    @GetMapping("/change")
    public RespBody infoStudyCollect(Long targetId , String collectType){
        if(targetId == null || StrUtil.isEmpty(collectType)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        MemberCollectDto memberCollectDto = new MemberCollectDto();
        memberCollectDto.setCollectType(collectType);//课程收藏
        memberCollectDto.setMemberId(WebUtil.currentMember().getId());
        memberCollectDto.setTargetId(targetId);
        List<MemberCollectDto> list = memberCollectService.listStudyCollect(memberCollectDto);
        String msg = "";
        if(list!=null && list.size()>0){
            // 收藏过了 取消收藏
            if (memberCollectService.removeById(list.get(0).getId())){
                msg = "取消收藏成功";
            }else {
                msg = "取消收藏失败";
            }

        }else {
            memberCollectDto.setCollectTime(new Date());
            if ( memberCollectService.save(ConvertUtil.transformObj(memberCollectDto , MemberCollectEntity.class))){
                msg = "收藏成功";
            }else {
                msg = "收藏失败";

            }
        }
        return RespBody.data(msg);
    }


}
